import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/page/message/component/messageListCom.dart';
import 'package:flutter_app/selfPlugin/selfUpAndDown.dart';

class SearchPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _searchState();
  }

}
// ignore: camel_case_types
class _searchState extends State{
  TextEditingController _controller;
  ScrollController sController;
  List listData = [
    {"title":"系统信息","content":"app软件即将更新！","time":"11:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"12:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"13:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"14:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"15:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"16:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"17:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"18:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"19:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"20:20"},
    {"title":"系统信息","content":"app软件即将更新！","time":"21:20"}
  ];
  List newListData = [
    {"title":"其他信息","content":"app软件即将更新！","time":"11:20"},
    {"title":"其他信息","content":"app软件即将更新！","time":"12:20"},
    {"title":"其他信息","content":"app软件即将更新！","time":"13:20"},
    {"title":"其他信息","content":"app软件即将更新！","time":"14:20"},
    {"title":"其他信息","content":"app软件即将更新！","time":"15:20"},
    {"title":"其他信息","content":"app软件即将更新！","time":"16:20"},
    {"title":"其他信息","content":"app软件即将更新！","time":"17:20"},
    {"title":"其他信息","content":"app软件即将更新！","time":"18:20"},
    {"title":"其他信息","content":"app软件即将更新！","time":"19:20"},
    {"title":"其他信息","content":"app软件即将更新！","time":"20:20"}
  ];
  int num = 1;
  bool _hasPage = true;


  void initState() {
    super.initState();
    _controller = TextEditingController();
    sController = ScrollController();
  }
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: PreferredSize(child: Stack(
          alignment: AlignmentDirectional.bottomCenter,
          fit: StackFit.loose,
          children: <Widget>[
            AppBar(

            ),
            Container(
              padding: EdgeInsets.only(left: 40,top: 23),
              child: new TextField(
                  textInputAction: TextInputAction.search,//输入法右下角是搜索突变
                  controller:_controller ,
                  maxLines: 1,
                  decoration: InputDecoration(
                    hintStyle: TextStyle(
                      color: Colors.white,
                      fontSize: 14,
                    ),
                    hintText: '请输入搜索内容',
                    suffixIcon: InkWell(
                      child: Icon(
                        Icons.close,
                        color: Colors.white,
                        size: 18,
                      ),
                      onTap: (){
                        _controller.clear();
                      },
                    ),
                    counterStyle: TextStyle(
                      color: Colors.white
                    ),
                    prefixIcon: Icon(
                      Icons.search,
                      color: Colors.white,
                      size: 18,
                    ),
                    filled: true,
                    fillColor: Colors.transparent,
                    border: InputBorder.none,
                  ),
                  onChanged: (str) {
                    print(str);
                  }),
            )
          ],
        ),
          preferredSize: Size.fromHeight(44),
        ),
        body:SelfUpAndDown(
          sController:sController,
          buildItem: _bulidItem,
          listData: listData,
          startDownFresh: _startDownFresh,
          startUpFresh:_startUpFresh,
          hasPage: _hasPage,
        )
    );
  }
  _bulidItem(index){
    //创建列表
    return Container(
      padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
      margin: EdgeInsets.only(top:10),
      color: Color.fromRGBO(245, 245, 245, 1),
      child: MessageListCom(listData[index]['title'],listData[index]['content'],listData[index]['time']),
    );
  }
  //开始下拉
  Future _startDownFresh(){
     return Future.delayed(Duration(seconds: 3),(){
      var list = new List();
      list.addAll(this.newListData);
      setState(() {
        this.listData = list;
        this.num = 1;
        this._hasPage = true;
      });
    });
  }
  //开始上拉
  Future _startUpFresh(){
    return Future.delayed(Duration(seconds: 1),(){
      if(this.num > 2){
        setState(() {
          this._hasPage = false;
        });
      }else{
        setState(() {
          this.listData.add({"title":"其他信息","content":"app软件即将更新！","time":"20:20"});
          this.num++;
          this._hasPage = true;
        });
      }
    });
  }
}